﻿using System;
using EmbeddedSharp.Core;
using EmbeddedSharp.Scripting;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Zove.Runtime.Test {
    public abstract class AbstractTests {

        public IContext Execute(string source) {
            ScriptingObject intp = new ScriptingObject(source, new ScriptingContext());
            intp.Run();

            return intp.Context;
        }

        public void AssertExpression(string expr, object expected) {
            Expression e = new Expression(expr);

            object result = e.Evaluate();
            if (result is Double) {
                Assert.AreEqual(
                    Math.Round(Convert.ToDouble(
                    expected), 5), Math.Round(Convert.ToDouble(
                    result), 5));
            } else if (result is Int32) {
                Assert.AreEqual(Convert.ToInt32(
                    expected), Convert.ToInt32(
                    result));
            } else if (result is string) {
                Assert.AreEqual(result.ToString(), expected.ToString());
            } else if (result is Boolean) {
                Assert.AreEqual(Convert.ToBoolean(result), Convert.ToBoolean(expected));
            } else {
                Assert.AreEqual(result, expected);
            }
        }
    }
}
